﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    /// <summary>
    /// 房屋管理
    /// create by liuyunfei at 2017/03/24
    /// </summary>
    public class Zor_Housing_Bll<T> where T : class
    {
        /// <summary>
        /// 获取房屋列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model ,int index, out int TotalCount, string where)
        {
            var uid = RequestSession.GetSessionUser().User_Id;
            var sqlSurch = string.Empty;
            if(Model.Hs_Number != null)
            {
                sqlSurch += "Hs_Number = '"+ Model.Hs_Number +"' and ";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " NatPer_Name like '%"+Model.NatPer_Name+"%' and ";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " NatPer_IdNumber like '%"+ Model.NatPer_IDNumber +"%' and ";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " NatPer_Phone like '%"+Model.NatPer_Phone + "%' and ";
            }
            if (Model.NatSex != null)
            {
                sqlSurch += " NatSex = '"+Model.NatSex + "' and ";
            }
            if (Model.HousUse != null)
            {
                sqlSurch += " HousUse like '%"+ Model.HousUse + "%' and ";
            }
            if (Model.Nature != null)
            {
                sqlSurch += " Nature like '%" + Model.Nature + "%' and ";
            }
            if (Model.Attribute != null)
            {
                sqlSurch += " Attribute like '%" + Model.Attribute + "%' and ";
            }
            if (Model.Cqsx != null)
            {
                sqlSurch += "Cqsx = '"+Model.Cqsx+"' and ";
            }
            sqlSurch += "Hs_Flage = 2 and ( User_Id = " + uid + "  or  Dt_Id = " + RequestSession.GetSessionUser().DT_Id + " ) and Hs_Flage = 2";
            //return PagerHelp<T>.GetPagerList(index, "V_Zor_Hous", " Bd_Name collate Chinese_PRC_CS_AS_KS_WS,Uim_Name  collate Chinese_PRC_CS_AS_KS_WS  ", out TotalCount, sqlSurch);
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Hous", " Hs_CreateTime desc ", out TotalCount, sqlSurch);
        }

        /// <summary>
        /// 房屋管理-修改
        /// </summary>
        /// <param name="housing">房屋对象</param>
        /// <returns>应答</returns>
        public static BaseAnswer UpdateHousing(Zor_Hous housing, Zor_NaturalPerson naturalPerson)
        {
            BaseAnswer bar = Zor_Housing_DAL<T>.UpdateHousing(housing, naturalPerson);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败,请检查房屋名称是否重复！";
            return bar;
        }

        /// <summary>
        /// 房屋管理-新增
        /// </summary>
        /// <param name="housing">房屋对象</param>
        /// <returns>执行结果</returns>
        public static BaseAnswer InsertHousing(dynamic housing, int flage, Zor_NaturalPerson naturalPerson)
        {
            housing.Hs_CreateTime = TimeHelper.ToNowTimeStamp();
            housing.User_Id = RequestSession.GetSessionUser().User_Id;
            housing.Hs_Flage = flage;
            naturalPerson.NatPer_CreateName = RequestSession.GetSessionUser().User_Id;
            naturalPerson.NatPer_CreateTime = TimeHelper.ToNowTimeStamp();

            BaseAnswer bar = Zor_Housing_DAL<T>.InsertHousing(housing, naturalPerson);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "增加成功！" : "新增失败，请检查房屋名称是否重复！";
            return bar;
        }

        /// <summary>
        /// 租赁信息新增
        /// </summary>
        /// <param name="lease">房屋对象</param>
        /// <returns>执行结果</returns>
        public static BaseAnswer InsertHsLease(Zor_Lease lease, DateTime startTime, DateTime endTime)
        {
            BaseAnswer bar = Zor_Housing_DAL<T>.InsertHsLease(lease, startTime, endTime);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "租赁成功！" : "租赁失败，请检查该房屋是否已处于租赁状态！";
            return bar;
        }
  

        /// <summary>
        /// 房屋删除
        /// </summary>
        /// <param name="id">房屋ID集合</param>
        /// <returns></returns>
        public static BaseAnswer DeleteHousing(int[] id)
        {
            BaseAnswer bar = Zor_Housing_DAL<T>.DeleteHousing(id);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "删除成功！" : "删除失败，请重试！";
            return bar;
        }

        /// <summary>
        /// 查询所有单元
        /// </summary>
        /// <returns></returns>
        public static List<T> GetBdGround(int id)
        {
            return Zor_Housing_DAL<T>.GetBdGround(id);
        }
        /// <summary>
        /// 查询所有街道
        /// create light by 2017.3.27     
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectBdName()
        {
            return Zor_Housing_DAL<T>.SelectBdName();
        }
        /// <summary>
        /// 查询所有门牌
        /// create light by 2017.3.28
        /// <param name="id">街道ID</param>
        /// </summary>
        /// <returns></returns>
        public static List<T> GetUimName(int Id)
        {
            return Zor_Housing_DAL<T>.GetUimName(Id);
        }

        /// <summary>
        /// 查询所有楼宇
        /// create  
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectBuildingName()
        {
            return Zor_Housing_DAL<T>.SelectBuildingName();
        }
        /// <summary>
        /// 获取房屋管理列表（不分页）
        /// create 刘云飞 by 2017/04/06       
        /// </summary>
        /// <returns></returns>
        public static List<T> GetList(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;
            if (Model.Hs_Number != null)
            {
                sqlSurch += "Hs_Number = '" + Model.Hs_Number + "' and ";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " NatPer_Name like '%" + Model.NatPer_Name + "%' and ";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " NatPer_IdNumber like '%" + Model.NatPer_IDNumber + "%' and ";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " NatPer_Phone like '%" + Model.NatPer_Phone + "%' and ";
            }
            if (Model.NatSex != null)
            {
                sqlSurch += " NatSex = '" + Model.NatSex + "' and ";
            }
            if (Model.HousUse != null)
            {
                sqlSurch += " HousUse like '%" + Model.HousUse + "%' and ";
            }
            if (Model.Nature != null)
            {
                sqlSurch += " Nature like '%" + Model.Nature + "%' and ";
            }
            if (Model.Attribute != null)
            {
                sqlSurch += " Attribute like '%" + Model.Attribute + "%' and ";
            }
            if (Model.Cqsx != null)
            {
                sqlSurch += "Cqsx = '" + Model.Cqsx + "' and ";
            }
            sqlSurch += "Hs_Flage = 2";
            return Zor_Housing_DAL<T>.SelectViewHousing(sqlSurch);
        }
        /// <summary>
        /// 获取商铺管理列表（不分页）
        /// </summary>
        /// <returns></returns>
        public static List<T> GetShopsList(dynamic model, string where)
        {
            var sqlSurch = string.Empty;

            if (model.Hs_Number != null)
            {
                sqlSurch += " Hs_Number like '%" + model.Hs_Number + "%' and ";
            }

            if (model.Bd_Name != null)
            {
                sqlSurch += " Bd_Name like '%" + model.Bd_Name + "%' and ";
            }
            if (model.Uim_Name != null)
            {
                sqlSurch += " Uim_Name like '%" + model.Uim_Name + "%' and ";
            }
            if (model.NatPer_Name != null)
            {
                sqlSurch += " NatPer_Name like '%" + model.NatPer_Name + "%' and ";
            }
            if (model.NatPer_IDNumber != null)
            {
                sqlSurch += " NatPer_IDNumber like '%" + model.NatPer_IDNumber + "%' and ";
            }
            if (model.NatPer_Phone != null)
            {
                sqlSurch += " NatPer_Phone like '%" + model.NatPer_Phone + "%' and ";
            }
            if (model.Hs_Proportion != 0)
            {
                sqlSurch += " Hs_Proportion like '%" + model.Hs_Proportion + "%' and ";
            }
            if (model.Shop_Longitude != null)
            {
                sqlSurch += " Shop_Longitude like '%" + model.Shop_Longitude + "%' and ";
            }
            if (model.Shop_Latitude != null)
            {
                sqlSurch += " Shop_Latitude like '%" + model.Shop_Latitude + "%' and ";
            }
            if (model.Attribute != null)
            {
                sqlSurch += " Attribute like '%" + model.Attribute + "%' and ";
            }
            sqlSurch += " Hs_Flage = 1";
            return Zor_Housing_DAL<T>.SelectViewShops(sqlSurch);
        }
        
    }
}
